package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Appointment;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IBusinessService businessService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequestMapping("/list")
    @RequirePermission(name = "预约单查询",expression = "appointment:list")
    public String list(Model model,@ModelAttribute("qo") AppointmentQueryObject qo) {
        // 分页和过滤
        model.addAttribute("pageInfo", appointmentService.query(qo));
        // 门店回显
        model.addAttribute("stores", businessService.listAll());
        // 业务下拉框
        model.addAttribute("business", systemDictionaryService.queryBySn("business"));
        // 状态下拉框
        model.addAttribute("status", systemDictionaryService.queryBySn("status"));
        return "appointment/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "预约单删除",expression = "appointment:delete")
    public String delete(Long id) {
        if (id != null) {
            appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }

    // 确认到店按钮  无消费 更新状态 归档
    @RequestMapping("/updateStatus")
    public String updateStatus(Long id,int status) {
        appointmentService.updateStatus(id, status);
        return "redirect:/appointment/list";
    }

    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "预约单编辑",expression = "appointment:saveOrUpdate")
    public String saveOrUpdate(Appointment appointment){
        if (appointment.getId() != null) {
            appointmentService.update(appointment);
        } else {
            appointmentService.insert(appointment);
        }
        return "redirect:/appointment/list";
    }

    // 客户在官网预约
    @RequestMapping("/save")
    public String save(Appointment appointment) {
        appointmentService.insert(appointment);
        return "redirect:/index";
    }

}
